(function() {
  var currentCallbackId = 0;

  function callNative(method, data, callback) {
    // 生成一个唯一callbackId
    var callbackId = "nativeCallback_" + currentCallbackId++;

    if (callback) {
      // 给window添加callback
      window[callbackId] = function(result) {
        delete window[callbackId];
        callback(result);
      };
    }

    var stringData = JSON.stringify(data);
    if (window.androidBridge) {
      // android端传递三个参数
      window.androidBridge.callNative(callbackId, method, stringData);
    } else {
      // iOS不支持多参数，我们传递json对象
      window.webkit.messageHandlers.iOSBridge.postMessage({
        callbackId: callbackId,
        method: method,
        data: stringData
      });
    }
  }

  // 将方法挂到window.JSBridge下
  var JSBridge = (window.JSBridge = {});
  JSBridge.callNative = callNative;

  JSBridge.UI = {};
  JSBridge.UI.toast = function(message) {
    callNative("UI.toast", { message: message });
  };
  JSBridge.UI.alert = function(params) {
    callNative("UI.alert", params);
  };
  JSBridge.UI.confirm = function(params, callback) {
    callNative("UI.confirm", params, callback);
  };

  JSBridge.KVDB = {}
  JSBridge.KVDB.getInt = function(key, callback) {
    callNative("KVDB.getInt", { key: key }, callback);
  }
  JSBridge.KVDB.setInt = function(key, value) {
    callNative("KVDB.setInt", { key: key, value: value });
  }
  JSBridge.KVDB.getBool = function(key, callback) {
    callNative("KVDB.getBool", { key: key }, callback);
  }
  JSBridge.KVDB.setBool = function(key, value) {
    callNative("KVDB.setBool", { key: key, value: value });
  }
  JSBridge.KVDB.getString = function(key, callback) {
    callNative("KVDB.getString", { key: key }, callback);
  }
  JSBridge.KVDB.setString = function(key, value) {
    callNative("KVDB.setString", { key: key, value: value });
  }
  JSBridge.KVDB.getJSON = function(key, callback) {
    callNative("KVDB.getString", { key: key }, function(result){ 
      callback(JSON.parse(result)) 
    })
  }
  JSBridge.KVDB.setJSON = function(key, value) {
    callNative("KVDB.setString", { key: key, value: JSON.stringify(value) });
  }

  JSBridge.Camera = {}
  JSBridge.Camera.takePicture = function(callback) {
    callNative("Camera.takePicture", {}, callback)
  }
  JSBridge.Camera.takeVideo = function(callback) {
    callNative("Camera.takeVideo", {}, callback)
  }
  JSBridge.Image = {}
  JSBridge.Image.pickPhotos = function(callback) {
    callNative("Image.pickPhotos", {}, callback)
  }

  function resolveNavParams(params) {
    params = JSON.parse(JSON.stringify(params))
    let path = location.pathname
    if (!path) {
      params.url = location.protocol + "//" + location.host + "/" + params.page
    } else {
      params.url = location.protocol + "//" + location.host + path.substr(0, path.lastIndexOf('/') + 1) + params.page
    }
    return params
  }

  JSBridge.Navigation = {}
  JSBridge.Navigation.open = function(params, callback) {
    callNative("Navigation.open", resolveNavParams(params), callback)
  }
  JSBridge.Navigation.close = function(callback) {
    callNative("Navigation.close", {}, callback)
  }
  JSBridge.Navigation.push = function(params, callback) {
    if (window.androidBridge) {
      JSBridge.Navigation.open(params, callback)
    } else {
      callNative("Navigation.push", resolveNavParams(params), callback);
    }
  }
})();
